package com.cac.demo.service.msCommission;

import com.cac.demo.dao.msCommission.MsCommissionDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @className: MsCommissionService
 * @description: （描述这个类的作用）
 * @author: lqc
 * @date: 2023/9/20 10:49:49 周三
 * @version: 1.0
 */
@Slf4j
@Service
public class MsCommissionService {

    @Autowired
    private MsCommissionDao msCommissionDao;

    public PageInfo queryCommissionC(int startPage, int pageSize, HashMap queryParam){
        PageHelper.startPage(startPage,pageSize);
        List<HashMap> data = msCommissionDao.queryCommissionC(queryParam);
        PageInfo pageInfo = new PageInfo(data);
        return pageInfo;
    }

    //生成代办信息
    public void createCommission(String type, String groupCode) {
        //生成工艺变更代办信息
        if ("change".equals(type)) {
            this.AoChange(groupCode);
        } else if ("notLoading".equals(type)) {
            this.notLoading();
        } else if ("notDelivery".equals(type)) {
            this.notDelivery();
        }

    }

    //工艺更改
    public void AoChange(String groupCode) {
        HashMap session=this.session();
        //查询该组号下发生工艺变更的物料
        List<HashMap> materials = msCommissionDao.selectChangedAo(groupCode);
        for (HashMap mto1 : materials
        ) {
            mto1.put("IP", session.get("IP").toString());
            mto1.put("userCode", session.get("userCode").toString());
            msCommissionDao.insertCommission(mto1);
        }
        //删除的物料信息
        List<HashMap> materialss = msCommissionDao.selectDeleteAo(groupCode);
        for (HashMap mto2 : materialss
        ) {
            mto2.put("IP", session.get("IP").toString());
            mto2.put("userCode", session.get("userCode").toString());
            msCommissionDao.insertCommission(mto2);
        }
        log.info("结束");
    }

    //下了日计划未装车数据
    public void notLoading() {
        HashMap session=this.session();
        List<HashMap> materials = msCommissionDao.selectNotDeliveryMto();
        for (HashMap mto : materials
        ) {
            mto.put("IP", session.get("IP").toString());
//            mto.put("userCode", "session.get("userCode").toString()");
            msCommissionDao.insertCommission(mto);
        }
    }

    //下了日计划已装车未配送数据
    public void notDelivery() {
        HashMap session=this.session();
        List<HashMap> materials = msCommissionDao.selectDeliveryDMto();
        for (HashMap mto : materials
        ) {
            mto.put("IP", session.get("IP").toString());
//            mto.put("userCode", session.get("userCode").toString());
            msCommissionDao.insertCommission(mto);
        }
    }

    //获取IP  userCode
    public HashMap session(){
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        log.info("IP" + IP);
        String userCode = (String) request.getSession().getAttribute("account");
        HashMap session =new HashMap();
        session.put("IP",IP);
        session.put("userCode",userCode);
        return session;
    }

}
